<?php

/**
 * Radcodes - SocialEngine Module
 *
 * @category   Application_Extensions
 * @package    Cmspage
 * @copyright  Copyright (c) 2009-2010 Radcodes LLC (http://www.radcodes.com)
 * @license    http://www.radcodes.com/license/
 * @version    $Id$
 * @author     Vincent Van <vincent@radcodes.com>
 */

class Cmspage_AdminSettingsController extends Core_Controller_Action_Admin {
	public function indexAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'cmspage_admin_main', array (), 'cmspage_admin_main_settings' );
		
		$this->view->form = $form = new Cmspage_Form_Admin_Global ();
		
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = $form->getValues ();
			
			foreach ( $values as $key => $value ) {
				Engine_Api::_ ()->getApi ( 'settings', 'core' )->setSetting ( $key, $value );
			}
			
			$savedChangesNotice = Zend_Registry::get ( 'Zend_Translate' )->_ ( "Your changes were saved." );
			$form->addNotice ( $savedChangesNotice );
		}
	}
	
	public function categoriesAction() {
		$this->view->navigation = $navigation = Engine_Api::_ ()->getApi ( 'menus', 'core' )->getNavigation ( 'cmspage_admin_main', array (), 'cmspage_admin_main_categories' );
		
		$this->view->categories = Engine_Api::_ ()->cmspage ()->getCategories ();
	}
	
	public function addCategoryAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		
		// Generate and assign form
		$form = $this->view->form = new Cmspage_Form_Admin_Category ();
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array () ) );
		// Check post
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			// we will add the category
			$values = $form->getValues ();
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				// add category to the database
				// Transaction
				$table = Engine_Api::_ ()->getDbtable ( 'categories', 'cmspage' );
				
				// insert the cmspage category entry into the database
				$row = $table->createRow ();
				$row->user_id = 1;
				$row->category_name = $values ["label"];
				$row->description = $values ["description"];
				$row->save ();
				
				// change the category of all the cmspages using that category
				

				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('' ) ) );
		}
		
		// Output
		$this->renderScript ( 'admin-settings/form.tpl' );
	}
	
	public function editCategoryAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		$form = $this->view->form = new Cmspage_Form_Admin_Category ();
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array () ) );
		
		// Check post
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = $form->getValues ();
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				// edit category in the database
				// Transaction
				$row = Engine_Api::_ ()->cmspage ()->getCategory ( $values ["id"] );
				
				$row->category_name = $values ["label"];
				$row->description = $values ["description"];
				$row->save ();
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('' ) ) );
		}
		
		// Must have an id
		if (! ($id = $this->_getParam ( 'id' ))) {
			throw new Zend_Exception ( 'No identifier specified' );
		}
		
		// Generate and assign form
		$category = Engine_Api::_ ()->cmspage ()->getCategory ( $id );
		$form->setField ( $category );
		
		// Output
		$this->renderScript ( 'admin-settings/form.tpl' );
	}
	
	public function deleteCategoryAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		$id = $this->_getParam ( 'id' );
		
		$this->view->category_id = $id;
		
		$form = $this->view->form = new Cmspage_Form_Admin_Category_Delete ();
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array () ) );
		
		$form->new_category_id->setValue ( $id );
		
		// Check post
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = $form->getValues ();
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				// go through logs and see which cmspage used this category and set it to NEW CATEGORY
				

				$cmspageTable = $this->_helper->api ()->getDbtable ( 'cmspages', 'cmspage' );
				$select = $cmspageTable->select ()->where ( 'category_id = ?', $id );
				$cmspages = $cmspageTable->fetchAll ( $select );
				
				$new_category_id = $values ['new_category_id'] ? $values ['new_category_id'] : 0;
				if ($new_category_id == $id) {
					$new_category_id = 0;
				}
				
				// create permissions
				foreach ( $cmspages as $cmspage ) {
					$cmspage->category_id = $new_category_id;
					$cmspage->save ();
				}
				
				$row = Engine_Api::_ ()->cmspage ()->getCategory ( $id );
				// delete the cmspage category in the database
				$row->delete ();
				
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('' ) ) );
		}
		
		// Output
		$this->renderScript ( 'admin-settings/delete.tpl' );
	}
	
	public function moveCategoryAction() {
		// In smoothbox
		$this->_helper->layout->setLayout ( 'admin-simple' );
		
		$form = $this->view->form = new Cmspage_Form_Admin_Category_Move ();
		$form->setAction ( $this->getFrontController ()->getRouter ()->assemble ( array () ) );
		
		// Check post
		if ($this->getRequest ()->isPost () && $form->isValid ( $this->getRequest ()->getPost () )) {
			$values = $form->getValues ();
			
			$db = Engine_Db_Table::getDefaultAdapter ();
			$db->beginTransaction ();
			
			try {
				$from_category_id = $values ['from_category_id'];
				$to_category_id = $values ['to_category_id'];
				
				// go through logs and see which cmspage used this category and set it to NEW CATEGORY
				

				$cmspageTable = $this->_helper->api ()->getDbtable ( 'cmspages', 'cmspage' );
				$select = $cmspageTable->select ()->where ( 'category_id = ?', $from_category_id );
				$cmspages = $cmspageTable->fetchAll ( $select );
				
				foreach ( $cmspages as $cmspage ) {
					$cmspage->category_id = $to_category_id;
					$cmspage->save ();
				}
				
				$db->commit ();
			} 

			catch ( Exception $e ) {
				$db->rollBack ();
				throw $e;
			}
			$this->_forward ( 'success', 'utility', 'core', array ('smoothboxClose' => 10, 'parentRefresh' => 10, 'messages' => array ('' ) ) );
		}
		
		// Output
		$this->renderScript ( 'admin-settings/move.tpl' );
	}
	
	function orderCategoriesAction() {
		if (! $this->getRequest ()->isPost ()) {
			echo 'out';
			return;
		}
		$db = Engine_Db_Table::getDefaultAdapter ();
		$db->beginTransaction ();
		
		try {
			$categories = Engine_Api::_ ()->cmspage ()->getCategories ();
			foreach ( $categories as $category ) {
				$category->order = $this->getRequest ()->getParam ( 'admin_category_item_' . $category->category_id );
				$category->save ();
				echo "\n" . $category->category_id . '=' . $category->order;
			}
			
			$db->commit ();
		} catch ( Exception $e ) {
			$db->rollBack ();
			throw $e;
		}
		
		return;
	}
}